//
//  ZFChartRangeView.m
//  yameishe3
//
//  Created by pro on 2017/3/6.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "ZFChartRangeView.h"
@interface ZFChartRangeView ()
{
    NSArray *_rangeTitleArray;
    NSArray *_rangeColorArray;
    NSMutableArray *_selectArray;
}

@end
@implementation ZFChartRangeView
-(void)awakeFromNib
{
    [super awakeFromNib];
    _rangeTitleArray = [[NSArray alloc]initWithObjects:@"直接访问",@"邮件营销",@"联盟广告",@"视频广告",@"搜索引擎",@"百度",@"谷歌",@"必应",@"其他", nil];
     _rangeColorArray = [[NSArray alloc]initWithObjects:
                         RGBColor(193, 48, 45),
                         RGBColor(48, 69, 84),
                         RGBColor(99, 161, 168),
                         RGBColor(211, 129, 102),
                         RGBColor(145, 200, 175),
                         RGBColor(145, 160, 132),
                         RGBColor(200, 133, 43),
                         RGBColor(188, 162, 154),
                         RGBColor(110, 112, 116),nil];
    _selectArray = [[NSMutableArray alloc]initWithArray:_rangeTitleArray];

    [self setRangeView];
}

-(void)setRangeView
{
    for (int i = 0; i<_rangeColorArray.count; i++) {
        UIView *view = [self viewWithTag:100+i];
        view.clipsToBounds = YES;
        self.layer.cornerRadius = 1;
        view.backgroundColor = _rangeColorArray[i];
        
        UILabel *label = [self viewWithTag:1000+i];
        label.text = _rangeTitleArray[i];
        
        UIView *basicView = [self viewWithTag:i];
        [basicView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]];
        if (_callbackDidSelect) {
                _callbackDidSelect(nil,_selectArray);
        }
    }
}
-(void)tapAction:(UITapGestureRecognizer*)sender
{
    NSInteger tag = sender.view.tag;
    NSString *title = _rangeTitleArray[tag];
    UIView *view = [self viewWithTag:100+tag];
    UILabel *label = [self viewWithTag:1000+tag];
    if ([_selectArray containsObject:title]) {//隐藏该类数据
        [_selectArray removeObject:title];
        view.backgroundColor = RGBColor(204, 204, 204);
        label.textColor = RGBColor(204, 204, 204);
    }
    else//重新显示该类数据
    {
        [_selectArray insertObject:title atIndex:tag];//逻辑还有问题
        view.backgroundColor = _rangeColorArray[tag];
        label.textColor = [UIColor blackColor];
        
    }
     _callbackDidSelect(nil,_selectArray);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
